package com.ihome.android.util;

import android.content.Context;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.View.OnFocusChangeListener;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ihome.android.util.AppConsts.KeyType;
import com.ihome.android.util.ClearKey;
import com.ihome.android.util.DeleteKey;
import com.ihome.android.util.KeyFunction;
import com.ihome.android.util.LetterKey;
import com.ihome.android.util.NumberKey;
import com.ihome.android.util.SingleKeyActionItem;
import com.ihome.android.util.SingleKeyItem;
import com.ihome.android.util.log.LogUtil;
import com.ihome.android.academy.R;
import java.util.ArrayList;
import java.util.List;

public class CustomKeyboard {
	private final static String	TAG							= CustomKeyboard.class.getSimpleName();
	private final static int	MSG_KEYBOARD_INPUT			= 10;
	private final static int	MSG_FOCUS_SEARCHRESULT		= 11;
	private final static int	MSG_NOFOCUS_SEARCHRESULT	= 12;
	private final static String	INPUT_RESULT				= "input_result";
	private static Context		context						= null;
	private static GridLayout	gl;
	private View				pView						= null;
	private static Handler		uiHandler					= null;
	private LayoutInflater		inflater;
	public static String		searchKeys;
	private static int			clickedId					= 0;

	static {
		gl = null;
		searchKeys = "";
	}

	public CustomKeyboard(Context paramContext, Handler paramHandler1, GridLayout paramGridLayout, View parent) {
		searchKeys = "";
		context = paramContext;
		uiHandler = paramHandler1;
		gl = paramGridLayout;
		pView = parent;
		inflater = ((LayoutInflater) paramContext.getSystemService("layout_inflater"));
		clickedId = 0;
	}

	private void bindButtonDrawable(Context paramContext, View paramView, int paramInt1, int paramInt2) {
		StateListDrawable localStateListDrawable = new StateListDrawable();
		localStateListDrawable.addState(new int[] { android.R.attr.state_focused, android.R.attr.state_enabled }, SearchHelper.getDrawable(paramContext, paramInt2));
		if (android.os.Build.VERSION.SDK_INT < 16) {
			paramView.setBackgroundDrawable(localStateListDrawable);
		}
		else {
			paramView.setBackground(localStateListDrawable);
		}
	}

	private static void chanageFocusStatus(ViewGroup paramViewGroup, boolean paramBoolean) {
		for (int i = 0;; i++) {
			if (i >= paramViewGroup.getChildCount()) {
				return;
			}
			View localView1 = paramViewGroup.getChildAt(i);
			localView1.setFocusable(paramBoolean);
			localView1.setFocusableInTouchMode(paramBoolean);
			View localView2 = localView1.findViewById(R.id.lkeyButton);
			if (localView2 != null) {
				localView2.setFocusable(paramBoolean);
				localView2.setFocusableInTouchMode(paramBoolean);
			}
			View localView3 = localView1.findViewById(R.id.img_Key_Button);
			if (localView3 != null) {
				localView3.setFocusable(paramBoolean);
				localView3.setFocusableInTouchMode(paramBoolean);
			}
		}
	}

	public static boolean isShown() {
		LinearLayout localLinearLayout = LetterKey.lpan;
		boolean bool = false;
		if (localLinearLayout != null) {
			int i = LetterKey.lpan.getVisibility();
			bool = false;
			if (i == View.VISIBLE) {
				bool = true;
			}
		}
		return bool;
	}

	public static void selectKey(int paramInt) {
		if (LetterKey.clickView != null) {
			LetterKey.clickView.requestFocus();
			return;
		}
		gl.getChildAt(paramInt).requestFocus();
	}

	public static void sendCommand(Context paramContext, final AppConsts.SearchType paramSearchType, final String paramString, int id) {
		LogUtil.d(TAG, "SearchType = " + paramSearchType + "\t paramString = " + paramString);
		onKeyInput(paramSearchType, paramString);
		clickedId = id;
	}

	public static void sendCommandEX(Context paramContext, final AppConsts.SearchType paramSearchType, final String paramString) {
		LogUtil.d(TAG, "SearchType = " + paramSearchType + "\t paramString = " + paramString);
		onKeyInput(paramSearchType, paramString);
	}

	public static void sendMsg(boolean enable) {
		if (enable) {
			uiHandler.sendEmptyMessage(MSG_FOCUS_SEARCHRESULT);
		}
		else {
			uiHandler.sendEmptyMessage(MSG_NOFOCUS_SEARCHRESULT);
		}

	}

	public static void setKeyBoards(boolean paramBoolean) {
		if ((paramBoolean) && (LetterKey.lpan != null)) {
			LetterKey.lpan.setVisibility(View.GONE);
		}
		chanageFocusStatus(gl, paramBoolean);
	}

	public List<KeyFunction> getKeyBoards() {
		ArrayList<KeyFunction> localArrayList = new ArrayList<KeyFunction>();
		localArrayList.add(new NumberKey(context, new SingleKeyItem("1", 0)));
		localArrayList.add(new LetterKey(context, new SingleKeyItem("2", "ABC", new SingleKeyActionItem("2", "A", "B", "C"), 1), pView));
		localArrayList.add(new LetterKey(context, new SingleKeyItem("3", "DEF", new SingleKeyActionItem("3", "D", "E", "F"), 2), pView));
		localArrayList.add(new LetterKey(context, new SingleKeyItem("4", "GHI", new SingleKeyActionItem("4", "G", "H", "I"), 3), pView));
		localArrayList.add(new LetterKey(context, new SingleKeyItem("5", "JKL", new SingleKeyActionItem("5", "J", "K", "L"), 4), pView));
		localArrayList.add(new LetterKey(context, new SingleKeyItem("6", "MNO", new SingleKeyActionItem("6", "M", "N", "O"), 5), pView));
		localArrayList.add(new LetterKey(context, new SingleKeyItem("7", "PQRS", new SingleKeyActionItem("7", "P", "Q", "R", "S"), 6), pView));
		localArrayList.add(new LetterKey(context, new SingleKeyItem("8", "TUV", new SingleKeyActionItem("8", "T", "U", "V"), 7), pView));
		localArrayList.add(new LetterKey(context, new SingleKeyItem("9", "WXYZ", new SingleKeyActionItem("9", "W", "X", "Y", "Z"), 8), pView));
		localArrayList.add(new ClearKey(context, new SingleKeyItem(R.drawable.key_clear, R.drawable.clear_sel, 9)));
		localArrayList.add(new LetterKey(context, new SingleKeyItem("0", ".  @  _", new SingleKeyActionItem("0", ".", "@", "_"), 10), pView));
		localArrayList.add(new DeleteKey(context, new SingleKeyItem(R.drawable.del, R.drawable.del_sel, 11)));
		return localArrayList;
	}

	public void initView() {
		List<KeyFunction> localList = getKeyBoards();
		gl.removeAllViews();

		int i = 0;
		View localView = null;
		for (;;) {

			if (i >= localList.size()) {
				return;
			}

			KeyFunction localKeyFunction = (KeyFunction) localList.get(i);
			SingleKeyItem localSingleKeyItem = (SingleKeyItem) localKeyFunction.item;

			KeyType keyType = localKeyFunction.getKeyType();

			switch (keyType) {
			case Letter:
			case Number:
				localView = inflater.inflate(R.layout.key_btn, null);
				TextView localTextView1 = (TextView) localView.findViewById(R.id.tv_Key_Title);
				TextView localTextView2 = (TextView) localView.findViewById(R.id.tv_Key_Body);
				localTextView1.setText(localSingleKeyItem.getTitle());
				localTextView2.setText(localSingleKeyItem.getBody());
				LinearLayout localLinearLayout = (LinearLayout) localView.findViewById(R.id.lkeyButton);
				localLinearLayout.setOnClickListener(localKeyFunction.getOnClickListener());
				localLinearLayout.setTag(localSingleKeyItem);
				localLinearLayout.setOnFocusChangeListener(new OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {}
				});
				localLinearLayout.setOnKeyListener(new OnKeyListener() {
					@Override
					public boolean onKey(View v, int keyCode, KeyEvent event) {
						// TODO Auto-generated method stub
						if (keyCode != KeyEvent.KEYCODE_DPAD_CENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
							SingleKeyItem keyItem = (SingleKeyItem) v.getTag();
							String title = keyItem.getTitle();
							if (title.contains("1") || title.contains("0")) {
								uiHandler.sendEmptyMessage(MSG_FOCUS_SEARCHRESULT);
							}
							else {
								return false;

							}
						}

						return false;
					}
				});

				bindButtonDrawable(context, localLinearLayout, 0, R.drawable.key_sel);
				break;
			case Clear:
			case Del:
				localView = inflater.inflate(R.layout.key_img, null);
				ImageView localImageView = (ImageView) localView.findViewById(R.id.img_Key_Button);
				localImageView.setImageResource(localSingleKeyItem.getImg());
				localImageView.setOnClickListener(localKeyFunction.getOnClickListener());
				localImageView.setTag(localSingleKeyItem);

				localImageView.setOnFocusChangeListener(new OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {}
				});

				bindButtonDrawable(context, localImageView, 0, localSingleKeyItem.getImg_sel());
				break;
			default:
				break;
			}

			gl.addView(localView);
			i++;
		}
	}

	public static void removeKeys() {
		if (searchKeys.length() <= 1) {
			searchKeys = "";
		}
		else {
			searchKeys = searchKeys.substring(0, -1 + searchKeys.length());
		}
	}

	public static void addKeys(String paramString) {
		searchKeys += paramString;
	}

	public static void clearKeys() {
		searchKeys = "";
	}

	public static void onKeyInput(final AppConsts.SearchType paramSearchType, final String paramString) {
		switch (paramSearchType) {
		case Data:
			addKeys(paramString);
			break;
		case Del:
			removeKeys();
			break;
		case Clear:
			clearKeys();
			break;
		default:
			break;
		}

		LogUtil.d(TAG, "searchKeys ===" + searchKeys);
		Message msg = new Message();
		Bundle b = new Bundle();
		b.putString(INPUT_RESULT, searchKeys);
		msg.setData(b);
		msg.what = MSG_KEYBOARD_INPUT;
		uiHandler.sendMessage(msg);
	}

	public void removeView() {
		gl.removeAllViews();
	}

	public boolean requestFocus(boolean firstEnter) {
		if (firstEnter) {
			gl.getChildAt(0).requestFocus();
		}
		else {
			if (LetterKey.clickView != null) {
				LetterKey.clickView.requestFocus();
				LogUtil.e(TAG, "LetterKey.clickView.requestFocus();");
				return true;
			}
			gl.requestFocus();
			gl.getChildAt(clickedId).requestFocus();
			gl.getChildAt(clickedId).requestFocus();
		}
		return true;
	}
}